# -*- coding: utf-8 -*-

from openerp.osv import osv
from openerp.osv import fields


EVALUATION_STAR_LEVEL = [
    ('1', u'一星'),
    ('2', u'二星'),
    ('3', u'三星'),
    ('4', u'四星'),
    ('5', u'五星'),
]


class order_evaluation(osv.osv):
    _name = 'order.evaluation'
    _rec_name = 'user_id'
    _order = 'id desc'

    _columns = {
        'user_id': fields.many2one('res.partner', u'用户'),
        'line_id': fields.many2one('sale.order.line', u'销售订单行'),
        'order_id': fields.many2one('sale.order', u'销售订单'),
        'star_ids': fields.one2many('order.evaluation.star', 'evaluation_id', u'评价星星'),
        'description': fields.text(u'评价描述'),
        'is_anonymous': fields.boolean(u'是否匿名'),
        'user_type': fields.selection([('seller', u'卖家'), ('buyer', u'买家')], u'评价者类型'),
    }

    _defaults = {
        'user_id': lambda self, cr, uid, ctx=None: ctx.get('user_id', False),
        # 'user_type': lambda self, cr, uid, ctx=None: ctx.get('user_type', '') == 'seller' and 'seller' or 'buyer',
        'user_type': 'buyer',
    }


class order_evaluation_star(osv.osv):
    _name = 'order.evaluation.star'
    _rec_name = 'star_config_id'

    _columns = {
        'evaluation_id': fields.many2one('order.evaluation', u'评价'),
        'star_config_id': fields.many2one('order.evaluation.star.config', u'名称'),
        'level': fields.selection(EVALUATION_STAR_LEVEL, string=u'等级'),
    }


class order_evaluation_star_config(osv.osv):
    _name = 'order.evaluation.star.config'

    def get_config_by_buyer(self, cr, uid, evaluation_id=None, context=None):
        res = []
        if evaluation_id and evaluation_id != 'false':
            evaluation = self.pool.get('order.evaluation').browse(cr, uid, evaluation_id, context=context)
            for star in evaluation.star_ids:
                res.append({
                        'id': star.star_config_id.id,
                        'name': star.star_config_id.name,
                        'level': star.level,
                    })
        else:
            res = self.search_read(cr, uid, [('user_type', '=', 'buyer')], ['name'], context=context) or False
            for val in res:
                val.update({'level': 5})

        return res

    _columns = {
        'name': fields.char(u'名称'),
        'user_type': fields.selection([('seller', u'卖家'), ('buyer', u'买家')], u'评价者类型'),
    }

    _defaults = {
        'user_type': 'buyer',
    }
